/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.loopj.hos.image.slice;

import com.loopj.hos.image.ResourceTable;
import com.loopj.hos.image.data.ListItemProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.PixelMap;
import ohos.utils.net.Uri;

import java.util.ArrayList;

public class PictureSelectAbilitySlice extends AbilitySlice implements ListItemProvider.OnItemClickListener {
    private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "PictureSelectAbilitySlice");
    private ListContainer listContainer;
    private ArrayList<PixelMap> pictures = new ArrayList<>();
    private ArrayList<Uri> urlList = new ArrayList<>();
    private ListItemProvider listItemProvider;

    public PictureSelectAbilitySlice(ArrayList<PixelMap> pixelMapArrayList, ArrayList<Uri> list) {
        pictures.clear();
        urlList.clear();
        pictures.addAll(pixelMapArrayList);
        urlList.addAll(list);
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_picture_select);
        initLayout();
        initPicture();
    }

    private void initPicture() {
        listItemProvider.setData(pictures, urlList);
    }


    private void initLayout() {
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_view);
        listItemProvider = new ListItemProvider(this, this);
        listContainer.setItemProvider(listItemProvider);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void OnItemClick(Uri pictureMo, int position) {
        Intent intent_picture_select = new Intent();
        intent_picture_select.setParam("picture_res_id", urlList.get(position).toString());
        setResult(intent_picture_select);
        terminate();
    }
}
